package com.zhanghe.study.design_pattern.iterator_pattern;

/**
 * @author zh
 * @date 2022/10/14 14:46
 */
public class ConcreteIterator<E> implements Iterator<E>{

    private ConcreteCollection<E> collection;

    // 记录当前索引位置
    private int index = 0;

    public ConcreteIterator(ConcreteCollection<E> collection){
        this.collection = collection;
    }

    @Override
    public void first() {
        index = 0;
    }

    @Override
    public E next() {
        int i = index;
        if(hasNext()){
            index =  i+1;
            return collection.get(i);
        }

        return null;

    }

    @Override
    public boolean hasNext() {
        if(index == collection.size()){
            return false;
        }
        return true;
    }
}
